<?php
class GalleriesController extends AppController {

	var $name = 'Galleries';
	var $uses = array('Gallery', 'Image');

	function index() {
		$this->Gallery->recursive = 0;
		$galleries = $this->paginate();
		$this->set(compact('galleries'));
		
		$primary_image = array();
		foreach ($galleries as $image) :
			$myimage = $this->Image->findById($image['Gallery']['primary_image']);
			$ext = $this->getExt($myimage['Image']['type']);
			$primary_image[$image['Gallery']['id']] = $myimage['Image']['filename'] . $ext;
		endforeach;
		$this->set(compact('primary_image'));		
	}

	function view($url = null) {
		if(!$url) {
			$this->Session->setFlash('Invalid Gallery.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$gallery = $this->Gallery->findByUrl($url);
		$ext = array();
		foreach ($gallery['Image'] as $image):
			$ext[$image['id']]['ext'] = $this->getExt($image['type']);
		endforeach;
		$this->set('gallery', $gallery);
		$this->set(compact('ext'));
	}

	function add() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Gallery->create();
			if($this->Gallery->save($this->data)) {
				$this->Session->setFlash('The Gallery has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Gallery could not be saved. Please, try again.');
			}
		}
		$images = $this->Gallery->Image->generateList();
		$this->set(compact('images'));
	}

	function edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Gallery');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Gallery->save($this->data)) {
				$this->Session->setFlash('The Gallery saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Gallery could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Gallery->read(null, $id);
		}
		$images = $this->Gallery->Image->generateList();
		$this->set(compact('images'));
	}

	function delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Gallery');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Gallery->del($id)) {
			$this->Session->setFlash('Gallery #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}


	function admin_index() {
		$this->Gallery->recursive = 0;
		$this->set('galleries', $this->paginate());
	}

	function admin_view($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid Gallery.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('gallery', $this->Gallery->read(null, $id));
	}

	function admin_add() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Gallery->create();
			if($this->Gallery->save($this->data)) {
				$this->Session->setFlash('The Gallery has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Gallery could not be saved. Please, try again.');
			}
		}
		$images = $this->Gallery->Image->generateList();
		$this->set(compact('images'));
	}

	function admin_edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Gallery');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Gallery->save($this->data)) {
				$this->Session->setFlash('The Gallery saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Gallery could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Gallery->read(null, $id);
		}
		$images = $this->Gallery->Image->generateList();
		$this->set(compact('images'));
	}

	function admin_delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Gallery');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Gallery->del($id)) {
			$this->Session->setFlash('Gallery #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}
	
	function rest($id=null,$galname=null,$return=5,$format='xml')
	{
		$layout = 'xml';
		if ($_POST['Gallery.id'])
		{
			$id = $this->data['Gallery.id'];
			$this->set('galleries', $this->Gallery->findById($id));
		}
	}
	
	// THis really needs to go into a component
	function getExt($type)
	{
		switch ($type) {
			case 'image/jpeg':
				$ext = ".jpg";
				break;
			case 'image/png':
				$ext = ".png";
				break;
			case 'image/gif':
				$ext = ".gif";
				break;
			default:
				$ext = ".jpg";
		}
		return $ext;
		
	}

}
?>